package com.ayq.activiti.manager.controller;


import com.ayq.activiti.common.util.Ret;
import com.ayq.activiti.manager.config.ActivitiIdGenerator;
import com.ayq.activiti.manager.domain.datadictionary.entity.ActivitiDataDictionary;
import com.ayq.activiti.manager.domain.datadictionary.entity.ActivitiDictionaryType;
import com.ayq.activiti.manager.domain.datadictionary.service.IActivitiDataDictionaryService;
import com.ayq.activiti.manager.domain.datadictionary.service.IActivitiDictionaryTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 *
 * <p>数据字典中心</p>
 * @Author ayq
 * @Date 2021/3/29 13:20
 */
@Controller
@RequestMapping("/activiti-data-dictionary")
@Api(tags = "数据字典中心")
@Slf4j
public class ActivitiDataDictionaryController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ActivitiDataDictionaryController.class);

    @Autowired
    private IActivitiDictionaryTypeService activitiDictionaryTypeService;
    @Autowired
    private IActivitiDataDictionaryService activitiDataDictionaryService;

    @Autowired
    private ActivitiIdGenerator activitiIdGenerator;

    @GetMapping("/adddatatype")
    public String getAddDataType(){
        return "model/adddatatype";
    }

    /**
     * <p>新增数据字典类型</p>
     * <p>方法名:addDataType</p>
     * @Author ayq
     * @Date 2021/3/30 10:44
     * @Param [activitiDictionaryType]
     * @Return org.ezca.esign.common.base.Ret
     */
    @PostMapping("/addDataType")
    @ResponseBody
    @ApiOperation(value = "新增数据字典类型", notes = "新增数据字典类型")
    public Ret addDataType(@RequestBody ActivitiDictionaryType activitiDictionaryType){
        activitiDictionaryType.setId(Long.parseLong(activitiIdGenerator.getNextId()));
        activitiDictionaryType.setCreateUsrId(1l);//当前用户ID  需修改
        activitiDictionaryType.setCreateTime(new Date());
        return Ret.ok(activitiDictionaryTypeService.save(activitiDictionaryType));
    }

    /**
     * <p>获取所有数据字典类型列表</p>
     * <p>方法名:dataTypelist</p>
     * @Author ayq
     * @Date 2021/3/30 10:56
     * @Param []
     * @Return org.ezca.esign.common.base.Ret
     */
    @GetMapping("/dataTypelist")
    @ResponseBody
    @ApiOperation(value = "获取所有数据字典类型列表", notes = "获取所有数据字典类型列表")
    public Ret dataTypelist(){
        QueryWrapper<ActivitiDictionaryType> tWrapper = new QueryWrapper<ActivitiDictionaryType>();
        tWrapper.orderByAsc("order_number");
        return Ret.ok(activitiDictionaryTypeService.list(tWrapper));
    }

    @GetMapping("/adddata/{typeId}")
    public String getAddData(@PathVariable("typeId") String typeId, Model model){
        model.addAttribute("typeId",typeId);
        return "model/adddata";
    }

    /**
     * <p>新增数据字典</p>
     * <p>方法名:addData</p>
     * @Author ayq
     * @Date 2021/3/30 11:00
     * @Param [activitiDataDictionary]
     * @Return org.ezca.esign.common.base.Ret
     */
    @PostMapping("/addData")
    @ResponseBody
    @ApiOperation(value = "新增数据字典", notes = "新增数据字典")
    public Ret addData(@RequestBody ActivitiDataDictionary activitiDataDictionary){
        activitiDataDictionary.setId(Long.parseLong(activitiIdGenerator.getNextId()));
        activitiDataDictionary.setCreateTime(new Date());
        return Ret.ok(activitiDataDictionaryService.save(activitiDataDictionary));
    }


    /**
     *
     * <p>根据id获取对应字典</p>
     * <p>方法名:getDataById</p>
     * @Author ayq
     * @Date 2021/3/30 13:45
     * @Param [id]
     * @Return org.ezca.esign.common.base.Ret
     */
    @GetMapping("/getDataById/{id}")
    @ResponseBody
    @ApiOperation(value = "根据id获取对应字典", notes = "根据id获取对应字典")
    public Ret getDataById(@PathVariable("id") String id){
        QueryWrapper<ActivitiDataDictionary> tWrapper = new QueryWrapper<ActivitiDataDictionary>();
        tWrapper.eq("type_id",id).eq("dictionary_status","0").orderByAsc("order_number");
        return Ret.ok(activitiDataDictionaryService.list(tWrapper));
    }

    @GetMapping("/dataList")
    public String getDataList(){
        return "model/datalist";
    }
}
